package com.flower.hooc.app.api.controller;

import com.flower.hooc.bean.pojo.entity.Cart;
import com.flower.hooc.bean.pojo.vo.RespResult;
import com.flower.hooc.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/hooc/cart")
@Api(tags = "购物业务接口")
public class CartController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(CartController.class);

    @Autowired//注入
    private CartService cartService;

    @GetMapping(value = "/showCart")
    public RespResult findCartByUserId(@RequestParam("userId") Long userId) {
        //System.out.println("/course/showAll controller is coming");
        System.out.println("showCart is dealing");
        List<Cart> carts = cartService.findCartByUserId(userId);
        return returnRespWithSuccess(carts);
    }

    @GetMapping(value = "/deleteCart")
    @ApiOperation("根据userId删除所有购物车表数据")
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID"
            , required = true, dataType = "long")
    public RespResult deleteCartByCid(@RequestParam("userId") Long userId) {
        System.out.println("delete cart");
        cartService.deleteCartByUid(userId);
        return returnRespWithSuccess(userId);
    }

    @GetMapping("/addCart")
    public RespResult addCart(long userId, long courseId, BigDecimal price) {
        System.out.println("userId is : " + userId);
        System.out.println("courseId is : " + courseId);
        System.out.println("price is : " + price);
        int result = cartService.addCart(userId, courseId, price);
        System.out.println("/addCart result is : " + result);
        RespResult respResult = new RespResult();
        respResult.setData("success");
        return respResult;
    }

}
